﻿using MES_SystemUI.Enumerate;
using NPOI.SS.Formula.PTG;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;

// MES_SystemUI.UI.DataDictUI.Derive
namespace MES_SystemUI.UI.DataDictUI
{
    public partial class Derive : Form
    {
        DataTable DataTable = new DataTable();
        public Derive()
        {
            InitializeComponent();
            textBox1.Enabled = false;
            comboBox1.Enabled = false;
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            button4.Enabled = false;
            radioButton1.Checked = true;
            checkBox1.Checked = false;
            checkBox1.CheckedChanged += CheckBox1_CheckedChanged;
            numericUpDown1.Enabled = false;
            numericUpDown2.Enabled = false;
            radioButton1.CheckedChanged += RadioButton1_CheckedChanged;
            radioButton3.CheckedChanged += RadioButton1_CheckedChanged;
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = " ";
            dateTimePicker1.ValueChanged += DateTimePicker1_ValueChanged;
            dateTimePicker2.ValueChanged += DateTimePicker2_ValueChanged;
            button4.Click += Button4_Click;
            button1.Click += Button1_Click;
            comboBox1.DataSource = new List<Enumerate.StatusEnum>() {
                StatusEnum.全部,
                StatusEnum.启用,
                StatusEnum.禁用,
            };
            comboBox1.SelectedIndex = 0;
        }

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                numericUpDown1.Enabled = true;
                numericUpDown2.Enabled = true;
            }
            else
            {
                numericUpDown1.Enabled = false;
                numericUpDown2.Enabled = false;
            }
            
        }

        private void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                textBox1.Enabled = true;
                comboBox1.Enabled = true;
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
                button4.Enabled = true;
            }
            else
            {
                textBox1.Text = "";
                comboBox1.Text = string.Empty;
                Button4_Click(null, null);
                textBox1.Enabled = false;
                comboBox1.Enabled = false;
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;
                button4.Enabled = false;
            }
        }
        public virtual void BindDataGridView()
        {
            // 获取总页数,每页十条数据
            using (var ct = new Context.MES_SystemContext())
            {
                var que = from Pc in ct.productCategories
                          join U1 in ct.users on Pc.CreateUserId equals U1.UserId
                          join U2 in ct.users on Pc.LastUpdateUserId equals U2.UserId into AllPc
                          from pc in AllPc.DefaultIfEmpty()
                          select new
                          {
                              序号 = Pc.ProductCategoryId,
                              类型名称 = Pc.ProductCategoryName,
                              状态 = Pc.Status == 0 ? "启用" : "禁用",
                              创建人 = U1.UserName,
                              创建时间 = Pc.CreateTime,
                              最后修改人 = pc.UserName != null ? pc.UserName : null,
                              最后修改时间 = Pc.LastUpdateTime,
                          };
                if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
                {
                    que = que.Where(x => Regex.IsMatch(x.类型名称, textBox1.Text));
                }
                if (!string.IsNullOrEmpty(dateTimePicker1.Text.Trim()))
                {
                    //DateTime dateTime1 = DateTime.Parse(dateTimePicker1.Value.ToString("yyyy-MM-dd") + " 00:00:00");
                    DateTime dateTime1 = DateTime.Parse(dateTimePicker1.Text + " 00:00:00");
                    que = que.Where(x => x.创建时间 >= dateTime1);
                }
                if (!string.IsNullOrEmpty(dateTimePicker2.Text.Trim()))
                {
                    //DateTime dateTime2 = DateTime.Parse(dateTimePicker2.Value.ToString("yyyy-MM-dd") + " 23:59:59");
                    DateTime dateTime2 = DateTime.Parse(dateTimePicker2.Text + " 23:59:59");
                    que = que.Where(x => x.创建时间 <= dateTime2);
                }
                if (comboBox1.Text != "全部")
                {
                    que = que.Where(x => x.状态 == comboBox1.Text);
                }
                que = que.OrderByDescending(k => k.序号);
                foreach (var item in que.ToList())
                {
                    Console.WriteLine(item.状态);
                }
            }
        }
        // 执行导出
        private void Button1_Click(object sender, EventArgs e)
        {
            BindDataGridView();
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void DateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "yyyy-MM-dd";
        }

        private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
        }
        private void Button4_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = " ";
        }
    }
}
